package com.xj.product.controller;

import com.xj.product.domain.Product;
import com.xj.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ProductController {
    @Value("${server.port}")
    private String port;

    @Autowired
    private ProductService productService;

    @GetMapping("/get/{id}")
    public String getProduct(@PathVariable("id") String id) throws Exception {
        System.out.println("传入的Id为："+id);
        throw new Exception("测试异常");
//        return port +"-"+id;
    }

    //商品信息查询
    @GetMapping("/product/{pid}")
    public Product findById(@PathVariable("pid") Integer pid) {
        Product product = productService.findByPid(pid);
        return product;
    }

    //减少库存
    @RequestMapping("/product/reduceInventory")
    public void reduceInventory(Integer pid, int num) {
        productService.reduceInventory(pid, num);
    }
}
